/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.web.ext;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

public class BindingSelectTests {
	private BindingSelect	binding	= new BindingSelect();

	@SuppressWarnings({ "rawtypes" })
	@Test
	public void checkProperties() {
		final ArrayList a = new ArrayList();
		final BindingSelect setOptions = this.binding.setOptions(a);

		assertEquals(this.binding, setOptions);
		assertEquals(a, this.binding.getOptions());

		final BindingSelect setDefaultDisplay = this.binding.setDefaultDisplay("Please pick a person");
		assertEquals(this.binding, setDefaultDisplay);
		assertEquals("Please pick a person", this.binding.getDefaultDisplay());

		final BindingSelect setDefaultValue = this.binding.setDefaultValue("");
		assertEquals(this.binding, setDefaultValue);
		assertEquals("", this.binding.getDefaultValue());

		final BindingSelect setDisplayMethod = this.binding.setDisplayMethod("firstName");
		assertEquals(this.binding, setDisplayMethod);
		assertEquals("firstName", this.binding.getDisplayMethod());

		final BindingSelect setValueMethod = this.binding.setValueMethod("id");
		assertEquals(this.binding, setValueMethod);
		assertEquals("id", this.binding.getValueMethod());
	}

	@Test
	public void checkDefaultSetting() throws IOException {
		this.binding.setDefaultDisplay("Please select");
		this.binding.setDefaultValue(" ");
		final MockHttpServletRequest request = new MockHttpServletRequest();
		final MockHttpServletResponse response = new MockHttpServletResponse();
		this.binding.render(request, response);
		final String contentAsString = response.getContentAsString();
		System.out.println(contentAsString);
	}

	@Test
	public void checkSimpleList() throws IOException {
		final List<Person> people = new ArrayList<Person>();
		final String selectedId = "12312123";
		people.add(new Person("Patrick", selectedId));

		this.binding.setOptions(people).setDisplayMethod("firstName")
										.setValueMethod("id")
										.setSelectedValue(selectedId);
		final MockHttpServletRequest request = new MockHttpServletRequest();
		final MockHttpServletResponse response = new MockHttpServletResponse();
		this.binding.render(request, response);
		final String contentAsString = response.getContentAsString();
		System.out.println(contentAsString);
		assertEquals(selectedId, this.binding.getSelectedValue());
		assertEquals("<select><option value=\"12312123\" selected=\"selected\">Patrick</option></select>", contentAsString);
	}

	@SuppressWarnings("unchecked")
	@Test(expected = IllegalArgumentException.class)
	public void checkInvalidProperty() throws IOException {
		@SuppressWarnings("rawtypes")
		final ArrayList options = new ArrayList();
		options.add(new Object());
		this.binding.setOptions(options);
		this.binding.setDisplayMethod("tim");
		final MockHttpServletRequest request = new MockHttpServletRequest();
		final MockHttpServletResponse response = new MockHttpServletResponse();
		this.binding.render(request, response);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Test(expected = IllegalStateException.class)
	public void checkListWithNoBinding() throws IOException {
		final ArrayList options = new ArrayList();
		options.add(new Object());
		this.binding.setOptions(options);
		final MockHttpServletRequest request = new MockHttpServletRequest();
		final MockHttpServletResponse response = new MockHttpServletResponse();
		this.binding.render(request, response);
	}
}
